package com.miss.spjyx

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

/**
 * 首次进入的设置页面。
 *
 * @author wl
 * @since 2019/08/05 23:22
 */
class PreferActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 首次打开，进入设置页：读取sp，查看是否已经设置过了
        val sp = getSharedPreferences("spj", Context.MODE_PRIVATE)
        val configured = sp.getBoolean("configured", false)
        if (configured) {
            startActivity(Intent(this@PreferActivity, IndexActivity::class.java))
            finish()
            return
        }
        setContentView(R.layout.activity_prefer)

        val et_ckdz = findViewById<EditText>(R.id.et_ckdz)
        val et_ck = findViewById<EditText>(R.id.et_ck)
        val et_ckd = findViewById<EditText>(R.id.et_ckd)
        val et_rkd = findViewById<EditText>(R.id.et_rkd)
        val btn_save = findViewById<Button>(R.id.btn_save)

        btn_save.setOnClickListener {
            if (et_ckdz.text.isNotEmpty() && et_ckd.text.isNotEmpty()
                && et_rkd.text.isNotEmpty() && et_ck.text.isNotEmpty()
            ) {
                sp.edit()
                    .putString("set_ckdz", et_ckdz.text.toString())
                    .putString("set_ck", et_ck.text.toString())
                    .putString("set_ckd", et_ckd.text.toString())
                    .putString("set_rkd", et_rkd.text.toString())
                    .putBoolean("configured", true)
                    .apply()
                startActivity(Intent(this@PreferActivity, IndexActivity::class.java))
                finish()
            } else {
                Toast.makeText(this@PreferActivity, "信息填写不完整", Toast.LENGTH_SHORT).show()
            }
        }
    }
}